<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #dource {
        width: 1000px;
        height: 200px;
        background-color: #d7a5a5;
        border: dashed 2px rgb(196, 109, 196);
        text-align: center;
        margin: 0 auto;
        color: rgb(159, 36, 36);
      }
    </style>
  </head>
  <body>
    <div id="dource" draggable="true">将文件拖到此处</div>
    <div id="dest"></div>
  </body>
  <script>
    // $(function () {});
    window.onload = function () {
      var dource = document.querySelector("#dource");
      var dest = document.querySelector("#dest");
      dource.addEventListener("dragover", function (e) {
        // 阻止默认事件
        e.preventDefault();
      });
      dource.addEventListener("drop", function (e) {
        e.preventDefault(); //赋值默认打开的事件
        var files = e.dataTransfer.files[0];
        // 判断是否为文本
        if (files.type === "text/plain") {
          var reader = new FileReader();
          reader.onload = function (e) {
            dest.innerHTML = e.target.result;
          };
          reader.readAsText(files);
        } else {
          dest.innerHTML = `请拖入文本`;
        }
      });
    };
  </script>
</html>
